﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicOnLine.Models;
using MusicOnLine.Common;
namespace MusicOnLine.Controllers
{ 
    [MOLAuthorzie]
    public class ManageSongController : Controller
    {
        private MusicPlayEntities db = new MusicPlayEntities();

        //
        // GET: /ManageSong/

        public ViewResult Index()
        {
            return View();
        }

        public ActionResult SongsList(string g, int PageIndex)
        {
            string s = HttpUtility.UrlDecode(g, System.Text.Encoding.GetEncoding("utf-8"));
            var query = db.AACInfoes.Where(o => o.Name.Contains(s) || o.Album.Contains(s) || o.Artist.Contains(s)).OrderByDescending(o => o.CreateDate);
            PageList<AACInfo> mode = new PageList<AACInfo>(query, PageIndex, 15);
            return Json(mode, JsonRequestBehavior.AllowGet);
        }

        public ActionResult QuickSearchSong(string term)
        {
            var mode = db.AACInfoes.Where(o => o.Name.Contains(term)).Select(o => new { value=o.Name}).Take(10).ToList();
            return Json(mode, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /ManageSong/Details/5

        public ViewResult Details(int id)
        {
            AACInfo aacinfo = db.AACInfoes.Find(id);
            return View(aacinfo);
        }

        //
        // GET: /ManageSong/Create

        public ActionResult Create()
        {            
            var GenreList = new SelectList(Common.Common.GetGenre(), "genre", "genre");
            ViewBag.GenreList = GenreList;
            return View();
        } 

        //
        // POST: /ManageSong/Create

        [HttpPost]
        public ActionResult Create(AACInfo songModel)
        {
            if (ModelState.IsValid)
            {
                var count = db.AACInfoes.Where(o => o.Album == songModel.Album && o.Name == songModel.Name&&o.AlbumArtist==songModel.AlbumArtist).Count();
                if (count <= 0)
                {
                    var file = Request.Files["fileLocation"];
                    if (file.FileName != "")
                    {
                        AACInfo aacinfo = new AACInfo();
                        string username = HttpContext.User.Identity.Name;
                        string ex = System.IO.Path.GetExtension(file.FileName);
                        string fileName = Guid.NewGuid().ToString() + ex;
                        string path = @"D:\Music\Songs\";
                        file.SaveAs(path + fileName);
                        var imgFile = Request.Files["fileImgUrl"];
                        if (imgFile != null)
                        {
                            string imgex = System.IO.Path.GetExtension(imgFile.FileName);
                            string imgfileName = Guid.NewGuid().ToString() + ex;
                            string pathImg = Server.MapPath("/AlbumImg/");
                            imgFile.SaveAs(pathImg + imgfileName);
                            aacinfo.IMGPath = imgfileName;
                        }

                        aacinfo.Album = songModel.Album;
                        aacinfo.AlbumArtist = songModel.AlbumArtist;
                        aacinfo.Artist = songModel.Artist;
                        aacinfo.Genre = songModel.Genre;
                        aacinfo.ReleaseDate = songModel.ReleaseDate;
                        aacinfo.Year = songModel.Year;
                        aacinfo.Location = fileName;
                        aacinfo.Name = songModel.Name;
                        aacinfo.PlayedCount = 0;
                        aacinfo.CreateBy = username;
                        aacinfo.ModifyBy = username;
                        db.AACInfoes.Add(aacinfo);
                        
                        db.SaveChanges();
                        return RedirectToAction("Details", "ManageSong", new { ID = aacinfo.ID });
                    }
                }
                ModelState.AddModelError("", "此歌曲已存在服务器上");
            }
            var GenreList = new SelectList(Common.Common.GetGenre(), "genre", "genre");
            ViewBag.GenreList = GenreList;
            return View(songModel);
        }
        
        //
        // GET: /ManageSong/Edit/5
 
        public ActionResult Edit(int id)
        {
            AACInfo aacinfo = db.AACInfoes.Find(id);
            ViewBag.ArtistList = new SelectList(db.Artists.Select(o => new { ID = o.ID, ArtistName = o.ArtistName }).ToList(), "ID", "ArtistName", aacinfo.ArtistID);
            ViewBag.AlbumList = new SelectList(db.Albums.Select(o => new { ID = o.ID, AlbumName = o.AlbumName }).ToList(), "ID", "AlbumName", aacinfo.AlbumID);
            return View(aacinfo);
        }

        //
        // POST: /ManageSong/Edit/5

        [HttpPost]
        public ActionResult Edit(AACInfo aacinfo)
        {
            if (ModelState.IsValid)
            {
                string username = HttpContext.User.Identity.Name;
                aacinfo.ModifyBy = username;
                aacinfo.ModifyDate = DateTime.Now;
                db.Entry(aacinfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(aacinfo);
        }

        //
        // GET: /ManageSong/Delete/5
 
        public ActionResult Delete(int id)
        {
            AACInfo aacinfo = db.AACInfoes.Find(id);
            return View(aacinfo);
        }

        //
        // POST: /ManageSong/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            AACInfo aacinfo = db.AACInfoes.Find(id);
            db.AACInfoes.Remove(aacinfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}